package com.mhl.campuscabinet;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager2.widget.ViewPager2;

import android.os.Bundle;

import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;
import com.mhl.adapter.FragmentAdapter;
import com.mhl.adapter.SoldProductAdapter;
import com.mhl.entity.SoldProduct;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

public class MyBuyActivity extends AppCompatActivity {

    private TabLayout tabLayout;
    private ViewPager2 viewPager2;
    List<String> titles = new ArrayList<>();
    List<Fragment> fragments = new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_buy);
        tabLayout = findViewById(R.id.tab_layout);
        tabLayout.setTabMode(tabLayout.MODE_SCROLLABLE);
        viewPager2 = findViewById(R.id.view_pager);
        // 添加标题
        titles.add("Completed");
        titles.add("Uncompleted");
        // 添加碎片
        fragments.add(new BoughtFragment());
        fragments.add(new BuyingFragment());
        // 实例化适配器
        FragmentAdapter fragmentAdapter = new FragmentAdapter(getSupportFragmentManager(),
                getLifecycle(), fragments);
        // 设置适配器
        viewPager2.setAdapter(fragmentAdapter);
        // 关联标签导航和ViewPager
        new TabLayoutMediator(tabLayout, viewPager2, new TabLayoutMediator.TabConfigurationStrategy() {
            @Override
            public void onConfigureTab(@NonNull @NotNull TabLayout.Tab tab, int position) {
                tab.setText(titles.get(position));
            }
        }).attach();
    }

}